/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.app.drawer;

import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.aafwk.ability.fraction.FractionScheduler;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.fragment.BinaryFragment;
import com.cv4j.app.fragment.FiltersFragment;
import com.cv4j.app.fragment.HistFragment;
import com.cv4j.app.fragment.HomeFragment;
import com.cv4j.app.fragment.IOFragment;
import com.cv4j.app.fragment.PixelOperatorFragment;
import com.cv4j.app.fragment.SpitalConvFragment;
import com.cv4j.app.fragment.TemplateMatchFragment;

import java.util.HashMap;

/**
 * MenuManager
 *
 * @since 2021-05-14
 */
public class MenuManager {
    private static MenuManager instance = null;
    FractionManager fragmentManager;
    HashMap<String, Fraction> fragmentHashMap;
    private MenuType curType;

    /**
     * MenuManager
     *
     * @param fragmentManager fragmentManager
     */
    private MenuManager(FractionManager fragmentManager) {
        this.fragmentManager = fragmentManager;
        fragmentHashMap = new HashMap<>();
    }

    /**
     * getCurType
     *
     * @return MenuType
     */
    public MenuType getCurType() {
        return curType;
    }

    /**
     * MenuType
     */
    public enum MenuType {
        HOME("cv4j", false),
        IO("io读写", true),
        FILTERS("常用滤镜", true),
        SPTIAL_CONV("空间卷积", true),
        BINARY("二值分析", true),
        HIST("直方图", true),
        TEMPLATE_MATCH("模版匹配", true),
        PIXEL_OPERATOR("像素操作", true);

        /**
         * 标题
         */
        public final String title;
        /**
         * 删除
         */
        public final boolean removed;

        MenuType(String title, boolean removed) {
            this.title = title;
            this.removed = removed;
        }

        public String getTitle() {
            return title;
        }

        public boolean isRemoved() {
            return removed;
        }
    }

    /**
     * getInstance
     *
     * @param fragmentManager fragmentManager
     * @return MenuManager
     */
    public static MenuManager getInstance(FractionManager fragmentManager) {
        return new MenuManager(fragmentManager);
    }

    /**
     * show
     *
     * @param type type
     * @return boolean
     */
    public boolean show(MenuType type) {
        if (curType != null && curType.equals(type)) {
            return true;
        }
        FractionScheduler fractionScheduler = fragmentManager.startFractionScheduler();
        fractionScheduler.replace(ResourceTable.Id_content_frame, create(type))
            .submit();

        curType = type;
        return true;
    }

    private Fraction create(MenuType type) {
        if (fragmentHashMap.containsKey(type.title)) {
            return fragmentHashMap.get(type.title);
        }
        Fraction fragment = null;
        switch (type) {
            case HOME:
                fragment = new HomeFragment();
                break;
            case IO:
                fragment = new IOFragment();
                break;
            case FILTERS:
                fragment = new FiltersFragment();
                break;
            case SPTIAL_CONV:
                fragment = new SpitalConvFragment();
                break;
            case BINARY:
                fragment = new BinaryFragment();
                break;
            case HIST:
                fragment = new HistFragment();
                break;
            case TEMPLATE_MATCH:
                fragment = new TemplateMatchFragment();
                break;
            case PIXEL_OPERATOR:
                fragment = new PixelOperatorFragment();
                break;
            default:
                break;
        }
        fragmentHashMap.put(type.title, fragment);
        return fragment;
    }
}
